<template>
  <div class="add-goods">
    <!-- 面包屑 -->
    <div class="title">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/goods' }"
          >商品管理</el-breadcrumb-item
        >
        <el-breadcrumb-item>添加商品</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 表单数据 -->
    <div class="myform">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label="类目选择" prop="category">
          <!-- <el-button type="primary">类目选择</el-button> -->
          <el-button text @click="dialogVisible = true">
            <el-button type="primary">类目选择</el-button>
          </el-button>
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="ruleForm.price" />
        </el-form-item>
        <el-form-item label="商品数量" prop="num">
          <el-input v-model="ruleForm.num" />
        </el-form-item>
        <el-form-item label="发布时间" required>
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker
                v-model="ruleForm.data1"
                type="date"
                label="选择日期"
                placeholder="选择日期"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col class="text-center" :span="2">
            <span class="text-gray-500">-</span>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="date2">
              <el-time-picker
                v-model="ruleForm.data2"
                label="选择时间"
                placeholder="选择时间"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="商品卖点" prop="sellPoint">
          <el-input v-model="ruleForm.sellPoint" />
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <el-button type="primary">上传图片</el-button>
        </el-form-item>
        <el-form-item label="商品描述" prop="descs">
          <textarea name="" id="" cols="40" rows="8"></textarea>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >确定</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 弹框--类目选择 -->
    <el-dialog v-model="dialogVisible" title="类目选择" width="30%">
      <div>
        <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" />
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogVisible = false">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
const dialogVisible = ref(false); //控制弹框是否出现

interface Tree {
  label: string
  children?: Tree[]
}

const handleNodeClick = (data: Tree) => {
  console.log(data)
}

const data: Tree[] = [
  {
    label: '家用电器',
    children: [
      {
        label: '电视',
        children: [
          {
            label: '彩电',
          },
          {
            label: '黑白',
          },
        ],
      },
      {
        label: '冰箱',
        children: [
          {
            label: '大冰箱',
          },
          {
            label: '小冰箱',
          },
        ],
      },
    ],
  },
  {
    label: '手机',
    children: [
      {
        label: '华为',
        children: [
          {
            label: '华为1',
          },
          {
            label: '华为2',
          },
        ],
      },
      {
        label: '苹果',
        children: [
          {
            label: '苹果1',
          },
          {
            label: '苹果2',
          },
        ],
      },
    ],
  },
  {
    label: '其他',
    
  },
]

const defaultProps = {
  children: 'children',
  label: 'label',
}

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  title: "",
  price: "",
  num: "",
  sellPoint: "",
  image: "",
  descs: "",
  category: "",
  data1: "",
  data2: "",
});

const rules = reactive<FormRules>({
  title: [
    { required: true, message: "请输入商品的名称", trigger: "blur" },
    { min: 2, max: 8, message: "长度2到8个", trigger: "blur" },
  ],
  price: [{ required: true, message: "请输入价格", trigger: "blur" }],
  num: [{ required: true, message: "请输入数量", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
</script>

<style lang="less" scoped>
.add-goods {
  margin: 20px;
}
.title {
  padding: 10px;
  background-color: #fff;
  border: 1px solid #eee;
  margin-bottom: 20px;
}
.myform {
  background-color: #fff;
  padding: 10px;
  padding-right: 30px;
}
.text-center {
  text-align: center;
}
</style>